package com.jkoss.wine.merchantOrder.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.UuidUtil;
import com.jkoss.wine.merchantOrder.entity.MerchantBasket;
import com.jkoss.wine.merchantOrder.entity.MerchantOrder;
import com.jkoss.wine.merchantOrder.mapper.MerchantBasketMapper;
import com.jkoss.wine.merchantOrder.mapper.MerchantOrderMapper;
import com.jkoss.wine.merchantOrder.service.IMerchantOrderService;
import com.jkoss.wine.merchantOrder.vo.MerchantOrderVo;
import com.jkoss.wine.shop.entity.ProductInventory;
import com.jkoss.wine.shop.entity.Shop;
import com.jkoss.wine.shop.mapper.ProductInventoryMapper;
import com.jkoss.wine.shop.mapper.ShopMapper;
import com.jkoss.wine.system.mapper.SystemSettingMapper;
import com.jkoss.wine.warehouse.entity.WareOutOrder;
import com.jkoss.wine.warehouse.mapper.WareInventoryMapper;
import com.jkoss.wine.warehouse.mapper.WareOutOrderMapper;

/**
 * 服务实现类
 * 
 * @Author Ning
 * @Version 1.0, 2018-10-05
 * @See
 * @Since com.jkoss.wine.merchantOrder.service.impl
 * @Description: TODO
 */
@Service
public class MerchantOrderServiceImpl extends ServiceImpl<MerchantOrderMapper, MerchantOrder> implements IMerchantOrderService {

	@Autowired
	private MerchantBasketMapper merchantBasketMapper;

	@Autowired
	private ProductInventoryMapper productInventoryMapper;

	// @Autowired
	// private WareInoutOrderDetailsMapper wareInoutOrderDetailsMapper;
	@Autowired
	private WareInventoryMapper wareInventoryMapper;
	@Autowired
	private SystemSettingMapper systemSettingMapper;
	// @Autowired
	// private WareInoutOrderMapper wareInoutOrderMapper;
	@Autowired
	private WareOutOrderMapper wareOutOrderMapper;
	@Autowired
	private ShopMapper shopMapper;

	@Override
	public Page selectOrderPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		List result = baseMapper.selectOrderPage(page, wrapper);
		page.setRecords(result);
		return page;
	}

	@Override
	public MerchantOrderVo selectVoOne(Wrapper wrapper) {
		return baseMapper.selectVoOne(wrapper);
	}

	@Override
	public List<MerchantOrderVo> selectVoByorid(Wrapper wrapper) {

		return this.baseMapper.selectVoByorid(wrapper);
	}

	@Override
	public Boolean insertMerchantBasket(MerchantOrder merchant_order) throws Exception {
		if (!retBool(baseMapper.updateById(merchant_order))) {
			throw new Exception("修改订单失败");
		}
		Wrapper wrapper = Condition.create().eq("merchant_order_id", merchant_order.getMerchantOrderId());
		List<MerchantBasket> baskets = merchantBasketMapper.selectList(wrapper);
		List<ProductInventory> inventories = new ArrayList<>();
		for (MerchantBasket item : baskets) {
			ProductInventory temp = new ProductInventory();
			temp.setProductInventoryId(UuidUtil.getUUID());
			temp.setBasketId(item.getMerchantBasketId());
			temp.setProductNormId(item.getShopNormId());
			temp.setProductId(item.getMerchantProductId());
			temp.setShopId(item.getShopId());
			temp.setInventory(item.getBasketCount());
			temp.setInventoryNum(null);
			temp.setType(0);
			temp.setOperator(null);
			temp.setAddTime(new Date());
			inventories.add(temp);
		}
		if (inventories.size() > 0) {
			if (!retBool(productInventoryMapper.insertBatch(inventories))) {
				throw new Exception("门店库存添加失败");
			}
		}
		// 完成订单，新客户转为常规客户
		Shop shop = shopMapper.selectById(merchant_order.getShopId());
		if (shop.getState() < 4) {
			// 设置为常规客户
			shop.setState(4);
			if (!retBool(shopMapper.updateById(shop))) {
				throw new Exception("店铺状态修改失败");
			}
		}
		return true;
	}

	@Override
	public List<MerchantOrderVo> selectVoByoridNormid(Wrapper wrapper) {
		// TODO Auto-generated method stub
		return this.baseMapper.selectVoByoridNormid(wrapper);
	}

	@Override
	public Page selectVoByshopid(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoByshopid(page, wrapper));
		return page;
	}

	@Override
	public boolean updateShip(MerchantOrder merchant_order) throws Exception {
		// 第一版
		// // 查询订单详情
		// Wrapper wrapper = Condition.create().eq("ioid",
		// merchant_order.getMerchantOrderId());
		// List<WareInoutOrderDetails> details =
		// wareInoutOrderDetailsMapper.selectList(wrapper);
		// // SystemSetting setting = new SystemSetting();
		// // setting.setKy("product_warning");
		// // setting = systemSettingMapper.selectOne(setting);
		//
		// // 扣减库存
		// for (WareInoutOrderDetails wareInoutOrderDetails : details) {
		// WareInventory entity = new WareInventory();
		// entity.setPnid(wareInoutOrderDetails.getPnid());
		// entity.setWhid(wareInoutOrderDetails.getWhid());
		// entity = wareInventoryMapper.selectOne(entity);
		// // if (CommonUtil.compareTo(entity.getAmut().toString(), setting.getVal()) >
		// 1)
		// // {
		// // throw new Exception("修改库存失败");
		// // }
		// entity.setAmut(entity.getAmut() - wareInoutOrderDetails.getAmut());
		// if (!retBool(wareInventoryMapper.updateById(entity))) {
		// throw new Exception("修改库存失败");
		// }
		// }

		// 第二版
		WareOutOrder inoutOrder = new WareOutOrder();
		inoutOrder.setOnum(merchant_order.getOrderNum());
		inoutOrder = wareOutOrderMapper.selectOne(inoutOrder);
		if (CommonUtil.isBlank(inoutOrder)) {
			throw new Exception("没有该订单的出货单据，请先添加出货单");
		}
		merchant_order.setOrderProcess(2);
		if (!retBool(baseMapper.updateById(merchant_order))) {
			throw new Exception("发货失败");
		}
		return true;
	}

	@Override
	public Page selectVoAccountType(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoAccountType(page, wrapper));
		return page;
	}

	@Override
	public Page selectOrderFlowPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		List result = baseMapper.selectOrderFlowPage(page, wrapper);
		page.setRecords(result);
		return page;
	}

	@Override
	public Page selectShopOL(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		List result = baseMapper.selectShopOL(page, wrapper);
		page.setRecords(result);
		return page;
	}

}
